package cn.tedu.charging.cost.dao.impl;

import cn.tedu.charging.common.pojo.param.ChargingProcessParam;
import cn.tedu.charging.cost.dao.repository.ChargingCostRuleCachRepository;
import cn.tedu.charging.cost.dao.repository.ChargingCostRuleRepository;
import cn.tedu.charging.cost.entity.po.ChargingCostRulePO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.List;


@Slf4j
@Repository
public class ChargingCostRuleCachRepositoryImpl implements ChargingCostRuleCachRepository {
    @Autowired
    private RedisTemplate<String, List<ChargingCostRulePO>> redisTemplate;

    @Autowired
    private ChargingCostRuleRepository chargingCostRuleRepository;

    @Override
    public  List<ChargingCostRulePO> getRulesByStationId(ChargingProcessParam chargingProcessParam) {
        ValueOperations<String, List<ChargingCostRulePO>> opsForValue = redisTemplate.opsForValue();
        List<ChargingCostRulePO> list = opsForValue.get("RULE_KEY_" + chargingProcessParam.getStationId());
        if (list != null){
            log.debug("走Redis");
            return list;
        }
        log.debug("走Mysql");
        List<ChargingCostRulePO> rulesByStationId = chargingCostRuleRepository.getRulesByStationId(chargingProcessParam);
        opsForValue.set("RULE_KEY_" + chargingProcessParam.getStationId(),rulesByStationId);
        return rulesByStationId;
    }
}
